﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PHTShop
{
    public partial class WebForm4 : basePageUser
    {
        private DataContextDataContext dc = new DataContextDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                switch (Session["languages"].ToString().ToLower())
                {
                    case "france":
                        Page.Title = "Spécial";
                        break;
                    case "germany":
                        Page.Title = "Besondere";
                        break;
                    default:
                        Page.Title = "Specials";
                        break;
                }
                this.ProductLanguage();


            }
        }

        protected void Page_PreInit(object sender, EventArgs e)
        {
            Setting th = this.dc.Settings.Single(t => t.type.Equals("theme") && Convert.ToInt32(t.value) == 1);
            Page.Theme = th.name.ToString();
        }

        private void ProductLanguage()
        {
            Language lg = this.dc.Languages.Single(t => t.name.Equals(Session["languages"]));

            List<Product> products = this.dc.Products.Where(t => t.status == true && t.features == true).ToList();

            List<ClassLoadSpecials> list = new List<ClassLoadSpecials>();

            foreach (var i in products)
            {
                Product pro = this.dc.Products.Single(t => t.id == i.id);
                ProductsLanguage prolg = this.dc.ProductsLanguages.Single(t => t.languageId == lg.id && t.productsId == i.id);
                string photoName = "";
                List<ProductsPhoto> productsPhoto = this.dc.ProductsPhotos.Where(t => t.productsId == i.id).ToList();
                foreach (var j in productsPhoto)
                {
                    ProductsPhoto propt = this.dc.ProductsPhotos.Single(t => t.id == j.id);
                    photoName = propt.name;
                }
                list.Add(new ClassLoadSpecials(pro, prolg, photoName));
            }


            this.ListViewProduct.DataSource = list;

            this.ListViewProduct.DataBind();
        }

        protected void ListViewProduct_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
        {
            this.DataPagerProduct.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
            this.ProductLanguage();
        }
    }
}